/**
 * 
 */
package kstech.com.wifitest.utils;
import android.app.Activity;
import android.content.Context;

import com.github.mikephil.charting.data.Entry;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import kstech.com.wifitest.Point;
import kstech.com.wifitest.config.DeviceModelFile;
import kstech.com.wifitest.config.ResConfigFile;
import kstech.com.wifitest.config.vo.CheckLineVO;

/**
 * App上下文，记录全局信息
 * @author tan
 *
 */
public class AppContext {

	public static Context context;

	/**
	 * 当前检线信息
	 */
	private static CheckLineVO currentCheckLine;

	/**
	 * 对应res_config.xml配置文件的内容
	 */
	private static ResConfigFile resConfig;

	/**
	 * 当前选中的机型对应的机型配置文件
	 */
	private static DeviceModelFile modelFile;

	/**
	 * 当前系统Activity信息
	 */
	private static List<Activity> acitvityList = new ArrayList<Activity>();

	/**
	 * 加载当前使用的机型文件
	 * @param deviceId 机型ID
	 * @param subDeviceId 子机型ID
	 * @throws ExcException
	 */
	public static void loadDeviceModelFile(String deviceId,String subDeviceId) throws Exception {
		String fileName = deviceId;
		if (null !=subDeviceId && !"".equals(subDeviceId)) {
			fileName = fileName + "%" + subDeviceId;
		}
		modelFile = DeviceModelFile.readFromFile(fileName+".xml");
	}

	public static void initResource() {
		resConfig = ResConfigFile.readFromFile();
	}

	public static DeviceModelFile getModelFile() {
		return modelFile;
	}

	public static void setModelFile(DeviceModelFile modelFile) {
		AppContext.modelFile = modelFile;
	}

	public static ResConfigFile getResConfig() {
		return resConfig;
	}

	public static CheckLineVO getCurrentCheckLine() {
		return currentCheckLine;
	}

	public static void setCurrentCheckLine(CheckLineVO checkLine) {
		AppContext.currentCheckLine = checkLine;
	}


	public static void addActivity(Activity acitvity) {
		acitvityList.add(acitvity);
	}

	/**
	 * 退出系统，结束所有activity的生命周期
	 */
	public static void exitSystem() {
		for (Activity acitvity : acitvityList) {
			acitvity.finish();
		}
	}
	public static void write(List<Entry> values, List<Entry> values2, String filename){
		// 收集崩溃日志, 可以在后台上传给服务器,供开发人员分析
		try {
			//将crash log写入文件
			FileOutputStream fileOutputStream = new FileOutputStream("/storage/sdcard1/"+filename+".txt", true);
			PrintStream printStream = new PrintStream(fileOutputStream);
			printStream.println("----------channel1----------共捕获了"+values.size()+"个值--------------");
			for (Entry value : values) {
				printStream.println("$data1"+value.getY());
			}
			printStream.println("---------------------channel1------------------------");

			printStream.println("----------channel2----------共捕获了"+values2.size()+"个值--------------");
			for (Entry value : values2) {
				printStream.println("$data2"+value.getY());
			}
			printStream.println("---------------------channel1------------------------");

			printStream.flush();
			printStream.close();
			fileOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void storeEntry(List<Entry> values, List<Entry> values2){
		LinkedList<Float> list1 = new LinkedList<>();
		LinkedList<Float> list2 = new LinkedList<>();
		for (Entry value : values) {
			list1.add(value.getY());
		}
		for (Entry value : values2) {
			list2.add(value.getY());
		}
		String s1 = JsonUtils.toJson(list1);
		String s2 = JsonUtils.toJson(list2);
		String text = "$s1$"+s1+"\r"+"$s2$"+s2;
		TextFile.write("/storage/sdcard1/linechart.txt",false,text);
	}
	public static String storeFloat(List<Float> values, List<Float> values2){
		int s = values.size()>values2.size() ? values.size(): values2.size();
		StringBuffer sb = new StringBuffer();
		sb.append("通道1"+"\t"+"通道2"+"\n");
		for (int i = 0; i < s; i++) {
			if (i < values.size()){
				sb.append(values.get(i)).append("\t");
			}else {
				sb.append("---\t");
			}

			if (i < values2.size()){
				sb.append(values2.get(i)).append("\n");
			}else {
				sb.append("---\n");
			}
		}
		String path = "/storage/sdcard1/linechart.txt";
		String text = sb.toString();

		return TextFile.write(path,false,text);
	}
	public static String storePoint(List<Point> values,List<Point> values2){
		int s = values.size()>values2.size() ? values.size(): values2.size();
		StringBuffer sb = new StringBuffer();
		sb.append("x1\t"+"通道1"+"\t"+"x2"+"\t"+"通道2"+"\n");
		for (int i = 0; i < s; i++) {
			if (i < values.size()){
				sb.append(values.get(i).getX()).append("\t").append(values.get(i).getValue()).append("\t");
			}else {
				sb.append("---\t").append("---\t");
			}

			if (i < values2.size()){
				sb.append(values2.get(i).getX()).append("\t").append(values2.get(i).getValue()).append("\n");
			}else {
				sb.append("---\t").append("---\n");
			}
		}
		String path = "/storage/sdcard1/20170110.txt";
		String text = sb.toString();

		return TextFile.write(path,false,text);
	}
}

